﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GenerateTile : MonoBehaviour
{
    //游戏中的瓦片对象
    public GameObject Floor;
    public GameObject Wall;
    public GameObject Box;
    public GameObject Target;
    public GameObject player;

    //关卡描述
    int[,] tiles1 = new int[8, 8]{
        {0,0,0,2,2,2,2,0},
        {0,2,2,2,1,1,2,0},
        {0,2,1,1,3,1,2,0},
        {2,2,1,2,1,1,2,2},
        {2,1,1,2,5,2,4,2},
        {2,1,2,1,1,3,4,2},
        {2,1,3,1,1,1,4,2},
        {2,2,2,2,2,2,2,2}
    };

    // Use this for initialization
    void Start()
    {
        Generate(tiles1);
    }

    // Update is called once per frame
    void Update()
    {

    }

    void Generate(int[,] tiles)
    {
        //获取第一维度的长度
        int m1 = tiles.GetLength(0);
        //获取第二维度的长度
        int m2 = tiles.GetLength(1);
        //获取对应于中心点的偏移值
        float offsetZ = m1 / 2;
        float offsetX = m2 / 2;


        for (int i = 0; i < m1; i++)
        {
            for (int j = 0; j < m2; j++)
            {
                //Debug.Log(i+" "+j);
                int tile = tiles[i, j];
                //适配矩阵并且x轴中央
                float z = offsetZ - i;
                float x = (m2 % 2 == 0 ? j - offsetX + 0.5f : j - offsetX);
                //生成对应的瓦片对象
                switch (tile)
                {
                    case 1:
                        Instantiate(Floor, new Vector3(x, -0.5f, z), Quaternion.Euler(90, 0, 0));
                        break;
                    case 2:
                        Instantiate(Wall, new Vector3(x, 0, z), Quaternion.identity);
                        break;
                    case 3:
                        Instantiate(Floor, new Vector3(x, -0.5f, z), Quaternion.Euler(90, 0, 0));
                        Instantiate(Box, new Vector3(x, 0, z), Quaternion.identity);
                        break;
                    case 4:
                        Instantiate(Floor, new Vector3(x, -0.5f, z), Quaternion.Euler(90, 0, 0));
                        Instantiate(Target, new Vector3(x, 0, z), Quaternion.identity);
                        break;
                    case 5:
                        Instantiate(Floor, new Vector3(x, -0.5f, z), Quaternion.Euler(90, 0, 0));
                        player.transform.position = new Vector3(x, 0, z);
                        break;
                }
            }
        }



    }
}
